<?php

namespace App\Http\Controllers\Seller;

use App\Http\Controllers\Controller;
use App\Http\Resources\Seller\AdvResource\AdvCollection;
use App\Models\Adv;
use App\Services\UploadService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class AdvController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @param Adv $adv_model
     * @return array|Response
     */
    public function index(Request $request, Adv $adv_model)
    {
        $store_id=$this->get_store(true);
        $adv_model = $adv_model->where(['store_id' => $store_id]);
        if (!empty($request->name)) {
            $adv_model = $adv_model->where('name', 'like', '%' . $request->name . '%');
        }
        if ($request->ap_id != '') {
            $adv_model = $adv_model->where('ap_id', 'like', '%' . $request->ap_id . '%');
        }
        $list = $adv_model->orderBy('id', 'desc')->paginate($request->per_page ?? 30);
        return $this->success(new AdvCollection($list));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @param Adv $adv_model
     * @return array|Response
     */
    public function store(Request $request, Adv $adv_model)
    {
        $adv_model->ap_id     = $request->ap_id ?? 0;
        $adv_model->name      = $request->name ?? '';
        $adv_model->url       = $request->url ?? '';
        $adv_model->image_url = $request->image_url ?? '';
        $adv_model->adv_start = $request->adv_start;
        $adv_model->adv_end   = $request->adv_end;
        $adv_model->adv_sort  = $request->adv_sort ?? 0;
        $adv_model->adv_type  = $request->adv_type ?? 0;
        $adv_model->store_id = $this->get_store(true);
        $adv_model->save();
        return $this->success([], __('base.success'));
    }

    /**
     * Display the specified resource.
     *
     * @param Adv $adv_model
     * @param int $id
     * @return array|Response
     */
    public function show(Adv $adv_model, int $id)
    {
        $info = $adv_model->find($id);
        return $this->success($info);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param Adv $adv_model
     * @param int $id
     * @return array|Response
     */
    public function update(Request $request, Adv $adv_model, int $id)
    {
        $adv_model            = $adv_model->find($id);
        $adv_model->ap_id     = $request->ap_id ?? 0;
        $adv_model->name      = $request->name ?? '';
        $adv_model->url       = $request->url ?? '';
        $adv_model->image_url = $request->image_url ?? '';
        $adv_model->adv_start = $request->adv_start;
        $adv_model->adv_end   = $request->adv_end;
        $adv_model->adv_sort  = $request->adv_sort ?? 0;
        $adv_model->adv_type  = $request->adv_type ?? 0;
        $adv_model->save();
        return $this->success([], __('base.success'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Adv $adv_model
     * @param int $id
     * @return array|Response
     */
    public function destroy(Adv $adv_model, int $id)
    {
        $idArray = array_filter(explode(',', $id), static function ($item) {
            return is_numeric($item);
        });
        $adv_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }
    /**
     * 广告图上传
     * @param UploadService $upload_service
     * @return array
     */
    public function adv_upload(UploadService $upload_service): array
    {
        $rs = $upload_service->adv();
        if ($rs['status']) {
            return $this->success($rs['data'], $rs['msg']);
        } else {
            return $this->error($rs['msg']);
        }
    }
}
